/*
 * 文件名：MCompanyBusiness.java
 * 版权：Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述： MCompanyBusiness.java
 * 修改人：yanshu
 * 修改时间：2016年1月11日
 * 修改内容：新增
 */
package com.feichuang.chineseWoodCarving.business.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.better517na.javaKeyIdGenerator.DatabaseDefinition;
import com.better517na.javaKeyIdGenerator.TableDefinition;
import com.feichuang.chineseWoodCarving.business.ICompanyBusiness;
import com.feichuang.chineseWoodCarving.dao.ICompanyDao;
import com.feichuang.chineseWoodCarving.dao.ImageDao;
import com.feichuang.chineseWoodCarving.model.MCompanyVo;
import com.feichuang.chineseWoodCarving.model.MImageVo;
import com.feichuang.chineseWoodCarving.model.MPagerRequestVo;
import com.feichuang.chineseWoodCarving.model.MPagerResultVo;

/**
 * TODO 企业信息业务处理.
 * <p>
 * TODO 详细描述
 * <p>
 * TODO 示例代码
 * 
 * <pre>
 * </pre>
 * 
 * @author yanshu
 */
@Component("companyBusiness")
public class CompanyBusinessImpl extends BaseBusinessImpl implements ICompanyBusiness {

    /**
     * 数据入口注入
     */
    @Resource(name = "companyDao")
    private ICompanyDao companyDao;

    /**
     * 图片数据入口
     */
    @Resource(name = "imageDao")
    private ImageDao imageDao;

    /**
     * {@inheritDoc}.
     */
    @Override
    public Response<?> insertMCompanyInfo(MCompanyVo companyVo) {
        // keyID生成组件（每个库每个表有自己独立的生成规则）,后面维护此代码的同学按照这个格式写就行。
        Response<?> res = new Response<MCompanyVo>();
        companyVo.setKeyId(keyIDFactory.NewKeyID(DatabaseDefinition.AutoRefundReport, TableDefinition.AdsManageSystemDB_PlatformInfo));
        if (companyDao.insertMCompanyInfo(companyVo)) {
            res = new Response<MCompanyVo>(StatusCode.SUCCESS, "完美操作！", null);
        } else {
            res = new Response<MCompanyVo>(StatusCode.ERROR, "风速过大", null);
        }
        return res;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Response<?> updateMCompanyInfo(MCompanyVo companyVo) {
        // TODO Auto-generated method stub
        Response<?> res = new Response<MCompanyVo>();
        if (companyDao.updateMCompanyInfo(companyVo)) {
            res = new Response<MCompanyVo>(StatusCode.SUCCESS, "完美操作！", null);
        } else {
            res = new Response<MCompanyVo>(StatusCode.ERROR, "风速过大", null);
        }
        return res;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public boolean deleteMCompanyInfo(MCompanyVo companyVo) {
        return companyDao.deleteMCompanyInfo(companyVo);
    }

    /**
     * {@查询企业基本信息包含图片信息}.
     */
    @Override
    public MCompanyVo selectMCompanyInfo(MCompanyVo companyVo) {
        MCompanyVo res = companyDao.selectMCompanyInfo(companyVo);
        List<MImageVo> images = imageDao.selectImagesByBelongID(res.getKeyId());
        res.setImages(images);
        return res;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public List<MCompanyVo> selectBatchMCompanyInfo(MCompanyVo companyVo) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * {@分页查询}.
     */
    @Override
    public MPagerResultVo<MCompanyVo> selectMCompanyInfoForPage(MPagerRequestVo<MCompanyVo> companyVo) {
        MPagerResultVo<MCompanyVo> res = new MPagerResultVo<MCompanyVo>();
        int size = companyVo.getSize();
        int currentPage = companyVo.getCurrentpage();
        if (size != 0 && currentPage != 0) {
            companyVo.setBegin((currentPage - 1) * size);
            companyVo.setEnd(size);
            int count = companyDao.selectMCompanyInfoCountForPage(companyVo.getDataModel());
            if (count == 0) {
                return res;
            }
            res.setTotalCount(count);
            res.setDataInfo(companyDao.selectMCompanyInfoForPage(companyVo));
        } else {
            return new MPagerResultVo<MCompanyVo>();
        }
        return res;

    }

    /**
     * {@模糊查询企业信息,此处略显蛋疼，懒得改了}.
     */
    @Override
    public MPagerResultVo<MCompanyVo> selectMCompanyVoFuzzyQuer(MCompanyVo companyVo, int currentpage, int size) {
        MPagerResultVo<MCompanyVo> res = new MPagerResultVo<MCompanyVo>();
        List<MCompanyVo> list = companyDao.selectMCompanyVoFuzzyQuer(companyVo);
        if (list != null && list.size() > 0) {
            int begin = (currentpage - 1) * size;
            int end = size;
            if (size != 0) {
                end = begin + size - 1;
                if (end > list.size()) {
                    end = list.size() - 1;
                }
            }
            List<MCompanyVo> ListNew = new ArrayList<MCompanyVo>();
            for (int i = begin; i <= end; i++) {
                ListNew.add(list.get(i));
            }
            // postListNew.addAll(postList.subList(begin, end));
            res.setTotalCount(list.size());
            res.setDataInfo(ListNew);
        }
        return res;
    }

}
